

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.damon.utils.DbUtil;

public class Wcs_printer_Run {
	static Logger logger = LoggerFactory.getLogger(Wcs_printer_Run.class);
	public static Map<String, Queue<String>> addressMap = new HashMap<>();
	public static void main(String[] args) {
		
		
		
		/*
		 * @output_number int output,
		 * @output_msg varchar(8000) output,
		 * @output_ip varchar(50) output,
		 * @output_port int output
		 */
		String sql = "{call sp_crerte_print_msg(?,?,?,?)}";
		Connection connection = null;
		CallableStatement callableStatement = null;
		int count;
		String msg;
		String ip;
		int port;
		try {
			connection = DbUtil.getConnection();
			callableStatement = connection.prepareCall(sql);
			callableStatement.registerOutParameter(1, Types.INTEGER);
			callableStatement.registerOutParameter(2, Types.VARCHAR);
			callableStatement.registerOutParameter(3, Types.VARCHAR);
			callableStatement.registerOutParameter(4, Types.INTEGER);
			
			String ipRegex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
			while(true) {
				Thread.sleep(100);
				long start = System.currentTimeMillis();

				
				callableStatement.execute();
				count = callableStatement.getInt(1);
				if (count == 0) {
					continue;
				}
				msg = callableStatement.getString(2);
				ip = callableStatement.getString(3);
				port = callableStatement.getInt(4);
				if (ip.matches(ipRegex)==false) {
					logger.error("ip格式错误！！");
					continue;
				}
				if (port<=0 || port>65535) {
					logger.error("端口超出范围！！");
				}
				String address = ip+":"+port;
				
				//socket创建成功时，才会将address添加到map里
				while(addressMap.containsKey(address) == false) {
					//启动该打印机对应线程
					
					Thread.sleep(3000);
				}
				Queue<String> queue = addressMap.get(address);
				//给每个address添加实例对象锁，既保证线程安全，又防止不同address的线程相互阻塞
				synchronized (queue) {
					queue.offer(msg);
				}
				long end = System.currentTimeMillis();
				System.out.println("p耗时："+(end-start));
			}
			
		} catch (SQLException e) {
			logger.error("循环读取数据异常！");
			e.printStackTrace();
		} catch (InterruptedException e) {
			logger.error("线程阻塞异常！");
			e.printStackTrace();
		}finally {
			try {
				callableStatement.close();
				connection.close();
				
				System.out.println("释放！！");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	
	class AddressThread extends Thread{
		Queue<String> queue;
		String address;
		
		public AddressThread(Queue<String> queue,String address) {
			this.queue = queue;
			this.address = address;
			this.setName(address);
		}
			@Override
			public void run() {
				String ip = address.split(":")[0];
				int port  = Integer.valueOf(address.split(":")[1]);
				try {
					Socket socket = new Socket(ip,port);
				} catch (Exception e) {
					logger.error("address: "+address+"，创建失败，确认服务器已打开！！");
					e.printStackTrace();
					return;
				}
				Queue<String> createQueue = new LinkedList<>();
				synchronized (addressMap) {
					addressMap.put(address, createQueue);
				}
				
				
				//如果map里没有该address的元素，则可能发生未知错误，应该将线程终止
				//正常情况不会发生该错误
				if(addressMap.containsKey(address)==false) {
					return;
				}
				Queue<String> queue = addressMap.get(address);
				
			}
		}
	
}
